/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.ui;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import phoside.Phoside;
import phoside.PhosideClassify;
import phoside.PhosideInit;
import phoside.PhosphoProteins;
import phoside.PhosphoProteinsImpl;

import phoside.classify.result.PhosidePredictionResult;

import phoside.disorder.DisorderPredictorVSL2;
import phoside.disorder.DisorderUtil;

import phoside.model.PhosideModel;
import phoside.model.PhosideModelManager;

import phoside.io.ProteinsReader;
import phoside.io.fasta.PhosphoReaderFromFastaWithSitesInSequence;
import phoside.io.fasta.FastaProteinsReaderFromString;
import phoside.io.fasta.PhosphoReaderFastaVisitorWithSitesInSequence;
import phoside.io.fasta.parser.DefaultPhosphoSequenceParser;
import phoside.io.xml.PhosideXmlProteinsReader;
import phoside.io.xml.DefaultProteinFieldValueFormatter;

import phoside.ui.task.DisorderReadTask;
import phoside.ui.task.DisorderPredictionTask;
import phoside.ui.task.ModelLoadingTask;
import phoside.ui.task.PhosideClassifyTask;
import phoside.ui.task.ProteinsReadTask;
import phoside.ui.task.TaskUtil;

import phoside.util.FileExtensionsFilter;
import phoside.util.FilePathParser;
import phoside.util.StaticFinalValues;

/**
 *
 * @author gjj
 */
public class PhosidePredictionPanel extends javax.swing.JPanel {

    /** Creates new form PhosidePredictionPanel */
    public PhosidePredictionPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JPanel modelPanel = new javax.swing.JPanel();
        modelComboBox = new javax.swing.JComboBox();
        inputTabbedPane = new javax.swing.JTabbedPane();
        seqPanel = new javax.swing.JPanel();
        javax.swing.JScrollPane seqScrollPane = new javax.swing.JScrollPane();
        seqTextArea = new javax.swing.JTextArea();
        fastaPanel = new javax.swing.JPanel();
        javax.swing.JPanel fastaFilePanel = new javax.swing.JPanel();
        fastaTextField = new javax.swing.JTextField();
        javax.swing.JButton fastaButton = new javax.swing.JButton();
        javax.swing.JPanel disorderPanel = new javax.swing.JPanel();
        disordeFileTextField = new javax.swing.JTextField();
        disorderFileButton = new javax.swing.JButton();
        xmlPanel = new javax.swing.JPanel();
        javax.swing.JPanel xmlFilePanel = new javax.swing.JPanel();
        xmlTextField = new javax.swing.JTextField();
        javax.swing.JButton xmlButton = new javax.swing.JButton();
        javax.swing.JPanel okPanel = new javax.swing.JPanel();
        predictButton = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(600, 310));
        setLayout(new java.awt.GridBagLayout());

        modelPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Select a Model"));
        modelPanel.setLayout(new javax.swing.BoxLayout(modelPanel, javax.swing.BoxLayout.LINE_AXIS));

        setModelComboBox();
        phoside.Phoside.getModelManager().getPropertyChangeSupport().addPropertyChangeListener(
            new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    setModelComboBox();
                }
            }
        );
        modelPanel.add(modelComboBox);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(modelPanel, gridBagConstraints);

        inputTabbedPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Protein Sequences"));

        seqPanel.setLayout(new javax.swing.BoxLayout(seqPanel, javax.swing.BoxLayout.LINE_AXIS));

        seqTextArea.setFont(new java.awt.Font("Monospaced",seqTextArea.getFont().getStyle(),14));
        seqTextArea.setColumns(20);
        seqTextArea.setLineWrap(true);
        seqTextArea.setRows(5);
        seqScrollPane.setViewportView(seqTextArea);

        seqPanel.add(seqScrollPane);

        inputTabbedPane.addTab("Sequence", seqPanel);

        fastaPanel.setLayout(new java.awt.GridBagLayout());

        fastaFilePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("FASTA file"));
        fastaFilePanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        fastaFilePanel.add(fastaTextField, gridBagConstraints);

        fastaButton.setText("Open");
        fastaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fastaButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        fastaFilePanel.add(fastaButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        fastaPanel.add(fastaFilePanel, gridBagConstraints);

        disorderPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("File containing predicted disorder scores [optional]"));
        disorderPanel.setLayout(new java.awt.GridBagLayout());

        disordeFileTextField.setToolTipText("Please select file containing the disorder scores");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        disorderPanel.add(disordeFileTextField, gridBagConstraints);

        disorderFileButton.setText("Open");
        disorderFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disorderFileButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        disorderPanel.add(disorderFileButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        fastaPanel.add(disorderPanel, gridBagConstraints);

        inputTabbedPane.addTab("FASTA", fastaPanel);

        xmlPanel.setLayout(new java.awt.GridBagLayout());

        xmlFilePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("PHOSIDE XML file"));
        xmlFilePanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        xmlFilePanel.add(xmlTextField, gridBagConstraints);

        xmlButton.setText("Open");
        xmlButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xmlButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        xmlFilePanel.add(xmlButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        xmlPanel.add(xmlFilePanel, gridBagConstraints);

        inputTabbedPane.addTab("PHOSIDE XML", xmlPanel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(inputTabbedPane, gridBagConstraints);

        predictButton.setText("    Submit    ");
        predictButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                predictButtonActionPerformed(evt);
            }
        });
        okPanel.add(predictButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(okPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void predictButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_predictButtonActionPerformed
        if (!verifyInput())
            return;

        // load model
        ModelFileWrapper mnw = (ModelFileWrapper)modelComboBox.getSelectedItem();
        ModelLoadingTask modelLoadingTask = new ModelLoadingTask(mnw.file());
        TaskUtil.execute(modelLoadingTask);
        if (!modelLoadingTask.success()) {
            JOptionPane.showMessageDialog(this, "Error: failed to load model.");
            return;
        }
        List<PhosideModel> models = modelLoadingTask.getModel();

        if (models==null || models.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Error: empty model.");
            return;
        }

        // read proteins
        PhosphoProteins proteins = new PhosphoProteinsImpl();
        ProteinsReader reader;

        String panelName;

        switch(inputTabbedPane.getSelectedIndex()) {
            case 0:
                String seq = seqTextArea.getText();
                seq = seq.trim();
                if (!seq.startsWith(">"))
                    seq = ">sequence\n"+seq;

                //seq = seq.replaceAll("[0-9\\p{Space}]", ""); // replace all digits and spaces, tabs, ...

                PhosphoReaderFastaVisitorWithSitesInSequence visitor
                        = new PhosphoReaderFastaVisitorWithSitesInSequence(proteins,
                            StaticFinalValues.DEFAULTHEADERRULE,
                            new DefaultPhosphoSequenceParser("?"));
                reader = new FastaProteinsReaderFromString(seq, visitor);
                
                Pattern p = Pattern.compile("[^\\p{Space}]+");
                Matcher m = p.matcher(seq.substring(1,21).trim());
                if (m.find()) {
                    panelName = m.group();
                    if (panelName.length()>20) {
                        panelName = panelName.substring(0,20)+"...";
                    }
                } else {
                    return;
                    //impossible state
                }

                break;
            case 1:
                String fasta = fastaTextField.getText();
                try {
                    reader = new PhosphoReaderFromFastaWithSitesInSequence(fasta, 
                            proteins, StaticFinalValues.DEFAULTHEADERRULE,
                            new DefaultPhosphoSequenceParser("?"));
                }catch (IOException e) {
                    JOptionPane.showMessageDialog(this, "Error when reading the fasta file.\n"+e.getMessage());
                    e.printStackTrace();
                    return;
                }

                panelName = FilePathParser.getName(fasta);
                break;
            case 2:
                String xml = xmlTextField.getText();
                reader = new PhosideXmlProteinsReader(xml, proteins,
                        new DefaultProteinFieldValueFormatter() {
                            @Override
                            public String write(String fieldName, Object fieldValue) {
                                return super.write(fieldName, fieldValue);
                            }

                            @Override
                            public Object read(String fieldName, String value) {
                                if (fieldName==null || value==null) {
                                    return null;
                                }

                                if (fieldName.compareTo(StaticFinalValues.QUERY)==0) {
                                    TreeSet<Integer> sites = new TreeSet();

                                    if (value.length()==0)
                                        return sites;

                                    String[] strs = value.split(StaticFinalValues.SEPARATOR);
                                    for (String str : strs) {
                                        sites.add(Integer.valueOf(str)-1);
                                    }

                                    return sites;
                                }

                                return super.read(fieldName, value);
                            }
                        });

                panelName = FilePathParser.getName(xml);
                break;
            default:
                return;//wrong state
        }

        ProteinsReadTask proteinReadTask = new ProteinsReadTask(reader);
        TaskUtil.execute(proteinReadTask);
        if (!proteinReadTask.success()) {
            JOptionPane.showMessageDialog(this, "Error when reading sequences.\n");
            return;
        }

        if (proteins.proteinCount()==0) {
            JOptionPane.showMessageDialog(this, "Error: no sequence was contained.\n");
            return;
        }

//        // clear sites
//        Iterator<Protein> it = proteins.proteinIterator();
//        while (it.hasNext()) {
//            Protein protein = it.next();
//            protein.removeInfo(StaticFinalValues.SITES);
//        }

        Set<String> accessions = proteins.getProteinsAccessions();
        boolean selectivePrediction = false;
        for (String acc : accessions) {
            Set<Integer> sites = proteins.getSites(acc);
            if (sites!=null && !sites.isEmpty()) {
                int option = JOptionPane.showConfirmDialog(this,
                        "Some residues in your input sequences were labeled for query.\n" +
                        "Would you like to predict on these labeled sites only?");
                if (option == JOptionPane.CANCEL_OPTION) {
                    return;
                } else if (option == JOptionPane.YES_OPTION) {
                    selectivePrediction = true;
                }
                break;
            }
        }

        // classify         
        boolean useDisorder = false;
        for (PhosideModel  model : models) {
            if (model.getModelProperties()
                    .getProperty(StaticFinalValues.USEDISORDERFEATURES)
                    .equalsIgnoreCase("true")) {
                useDisorder = true;
                break;
            }
        }

        // integrate disorder if used
        if (useDisorder) {
            boolean predict = true;
            if (inputTabbedPane.getSelectedIndex()==1) {
                String dis = disordeFileTextField.getText();
                if (dis!=null && dis.length()>0) {
                    DisorderReadTask readTask = new DisorderReadTask(dis);
                    TaskUtil.execute(readTask);
                    if (!readTask.success()) {
                        JOptionPane.showMessageDialog(this, "Failed to read disorder");
                        return;
                    }

                    Map<String,List<Double>> mapDis = readTask.getDisorder();
                    DisorderUtil.integrateDisorder(proteins, mapDis);
                    predict = false;
                }
            }

            if (predict) {
                DisorderPredictorVSL2 disorderPredictor = new DisorderPredictorVSL2();
                DisorderPredictionTask disTask = new DisorderPredictionTask(proteins, disorderPredictor);
                TaskUtil.execute(disTask);
                if (!disTask.success()) {
                    JOptionPane.showMessageDialog(this, "Error: failed to predict disorder.");
                    return;
                }
                DisorderUtil.integrateDisorder(proteins, disTask.getDisorder());
            }
        }

        // Execute Task in New Thread; pop open JTask Dialog Box.
        Map<String, PhosidePredictionResult> results = new LinkedHashMap();

        int jobSize = 50;
        PhosideClassify classify = new PhosideClassify();
        classify.setJobSize(jobSize);
        classify.setSelectivePrediction(selectivePrediction);

        int n = models.size();
        for (int i=0; i<n; i++) {
            PhosideModel model = models.get(i);

//            classify.setKeepSequenceInResult(i==0); // keep sequence information only in the first result
            PhosideClassifyTask classifyTask = new PhosideClassifyTask(model, proteins, classify);
            //PhosideClassifyTask classifyTask = new PhosideClassifyTask(model, proteins,jobSize, selectivePrediction);
            TaskUtil.execute(classifyTask);
            boolean succ = classifyTask.success();

            if (succ) {
                PhosidePredictionResult result = classifyTask.getResult();
                results.put(model.getName(), result);
            } else {
                //JOptionPane.showMessageDialog(this, "Error: failed to predict.");
                if (n>1) {
                    int op = JOptionPane.showConfirmDialog(this, "Failed to classify using model "
                            +model.getName()+"\nContinue?", "Continue?", JOptionPane.YES_NO_OPTION);
                    if (op == JOptionPane.NO_OPTION)
                        return;
                }
            }
        }

        if (results.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Failed to predict.");
            return;
        }

        // add result panel
        PhosideResultPanel resultPanel
                = new PhosideResultPanel(panelName, results);
        Phoside.getDesktop().addResultPanel(resultPanel);
    }//GEN-LAST:event_predictButtonActionPerformed

    private void fastaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fastaButtonActionPerformed
        JFileChooser fc = new JFileChooser(PhosideInit.defaultPath);
        ArrayList<String> exts = new ArrayList<String>(1);
        String ext = "FASTA";
        exts.add(ext);
        fc.setFileFilter(new FileExtensionsFilter(exts,"Fasta file (.fasta)"));
        //fc.setAcceptAllFileFilterUsed(true);
        fc.setDialogTitle("Select a Fasta file...");
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String filePath = file.getParent() + File.separator + file.getName();
            fastaTextField.setText(filePath);
            PhosideInit.defaultPath = file.getParent();
        }
}//GEN-LAST:event_fastaButtonActionPerformed

    private void disorderFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disorderFileButtonActionPerformed
        JFileChooser fc = new JFileChooser(PhosideInit.defaultPath);
        ArrayList<String> exts = new ArrayList<String>(1);
        String ext = StaticFinalValues.DISORDERAPPENIX;
        exts.add(ext);
        fc.setFileFilter(new FileExtensionsFilter(exts,"Fasta file (.fasta)"));
        //fc.setAcceptAllFileFilterUsed(true);
        fc.setDialogTitle("Select a disorder file...");
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            PhosideInit.defaultPath = file.getParent();

            String filePath = PhosideInit.defaultPath + File.separator + file.getName();
            disordeFileTextField.setText(filePath);
        }
}//GEN-LAST:event_disorderFileButtonActionPerformed

    private void xmlButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xmlButtonActionPerformed
        JFileChooser fc = new JFileChooser(PhosideInit.defaultPath);
        ArrayList<String> exts = new ArrayList<String>(1);
        String ext = "xml";
        exts.add(ext);
        fc.setFileFilter(new FileExtensionsFilter(exts,"PHSIDE XML file (.xml)"));
        //fc.setAcceptAllFileFilterUsed(true);
        fc.setDialogTitle("Select a PHOSIDE file...");
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String filePath = file.getParent() + File.separator + file.getName();
            xmlTextField.setText(filePath);
            PhosideInit.defaultPath = file.getParent();
        }
    }//GEN-LAST:event_xmlButtonActionPerformed

    public String getSelectedModel() {
        // load model
        Object obj = modelComboBox.getSelectedItem();
        if (obj==null)
            return null;

        if (modelComboBox.getSelectedIndex()==0)
            return null;

        return (String)obj;
    }

    public void setModelComboBox() {
        PhosideModelManager manager = Phoside.getModelManager();
        Vector items = new Vector(manager.modelFileCount()+1);
        items.add("Please select a trained model");
        for (String file : manager.modelFiles()) {
            items.add(new ModelFileWrapper(file));
        }
        String selected = (String)modelComboBox.getSelectedItem();
        modelComboBox.setModel(new javax.swing.DefaultComboBoxModel(items));
        if (selected!=null)
            modelComboBox.setSelectedItem(selected);
    }

    private boolean verifyInput() {
        if (modelComboBox.getSelectedIndex()<1) {
            JOptionPane.showMessageDialog(this, "Error: no model was selected." +
                    "\nPlease select one.");
            return false;
        }

        switch (inputTabbedPane.getSelectedIndex()) {
            case 0: //sequence
                String seq = seqTextArea.getText();
                if (seq==null || seq.length()==0) {
                    JOptionPane.showMessageDialog(this, "Error: the protein sequence cannot be empty.");
                    return false;
                }
                break;
            case 1: //fasta
                String fasta = fastaTextField.getText();
                if (fasta==null || fasta.length()==0) {
                    JOptionPane.showMessageDialog(this, "Error: the fasta file name cannot be empty.");
                    return false;
                }

                if (!phoside.util.IOUtil.fileExist(fasta)) {
                    JOptionPane.showMessageDialog(this, "Error: the fasta file does not exist.");
                    return false;
                }

                String dis = this.disordeFileTextField.getText();

                if (dis!=null && dis.length()!=0 && !phoside.util.IOUtil.fileExist(dis)) {
                    JOptionPane.showMessageDialog(this, "Error: the disorder file does not exist.");
                    return false;
                }
                break;
            case 2: // xml
                String xml = xmlTextField.getText();
                if (xml==null || xml.length()==0) {
                    JOptionPane.showMessageDialog(this, "Error: the xml file name cannot be empty.");
                    return false;
                }

                if (!phoside.util.IOUtil.fileExist(xml)) {
                    JOptionPane.showMessageDialog(this, "Error: the xml file does not exist.");
                    return false;
                }
                break;
            default:
                return false;
        }

        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField disordeFileTextField;
    private javax.swing.JButton disorderFileButton;
    private javax.swing.JPanel fastaPanel;
    private javax.swing.JTextField fastaTextField;
    private javax.swing.JTabbedPane inputTabbedPane;
    private javax.swing.JComboBox modelComboBox;
    private javax.swing.JButton predictButton;
    private javax.swing.JPanel seqPanel;
    private javax.swing.JTextArea seqTextArea;
    private javax.swing.JPanel xmlPanel;
    private javax.swing.JTextField xmlTextField;
    // End of variables declaration//GEN-END:variables

}
